package com.example.hblog.module.service.serviceImpl;

import com.example.hblog.module.dao.UserDao;
import com.example.hblog.module.pojo.User;
import com.example.hblog.module.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User userLogin(User user) {
        User userlog =userDao.userLogin(user);
        if(userlog != null){
            return userlog;
        }
        throw new RuntimeException("账号或密码错误!");
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<User> allUser() {
        return userDao.allUser();
    }

    @Override
    public int findUserLoginByPhone(String phone) {
        return 0;
    }

    @Override
    public void updateUser(User user) {
        try{
            userDao.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public User findUserByPhone(String phone) {
        return userDao.findUserByPhone(phone);
    }

    @Override
    public User mySelf() {
        return userDao.mySelf();
    }

//    @Override
//    public int findUserLoginByPhone(String userToken) {
//        try {
//            Map<String,Object> map = JWTUtils.verify1(userToken);
//            String phone = map.get("phone").toString();
//            String name = map.get("name").toString();
//            String level = map.get("level").toString();
//            System.out.println(phone+"  "+name+"   "+level);
//            User user = userDao.findUserLoginByPhone(phone);
//            System.out.println(user.getPhone()+"   "+user.getName()+"    "+user.getLevel());
//            if (user!=null&user.getName().equals(name)&&user.getLevel().equals(level)){
//                return 1;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
}
